<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="logo">
				<view class="title_img">
					<view class="title" v-if='islogin'>欢迎来到寻觅</view>
					<view class="title" v-else>感谢您注册寻觅</view>
					<image src="../../static/img/logo.png" mode=""></image>
				</view>
			</view>
			<u-form :model="loginFrom" :rules="loginFromRules" :error-type="errorType" ref="loginFrom" label-width="60px">

				<!-- 登录 -->
				<view v-if='islogin' key="login">
					<!-- 帐号 -->
					<u-form-item  prop="id" label="账号:" left-icon="account">
						<u-input placeholder='用户名或者邮箱号' style="width:100rpx;" focus v-model="loginFrom.id" />
					</u-form-item>
					<!-- 密码 -->
					<u-form-item prop="password" label="密码:" left-icon="lock">
						<u-input placeholder='请输入密码' type="password" v-model="loginFrom.password" />
					</u-form-item>
				</view>

				<!-- 注册 -->
				<view   v-else key="nologin">
					<!-- 邮箱 -->
					<u-form-item prop="email" label="邮箱:" left-icon="email">
						<u-input placeholder='请输入邮箱' style="width:100rpx;" v-model="loginFrom.email" />
					</u-form-item>
					<!-- 昵称 -->
					<u-form-item prop="nickname" label="昵称:" left-icon="edit-pen">
						<u-input placeholder='请输入昵称' style="width:100rpx;" v-model="loginFrom.nickname" />
					</u-form-item>
					<!-- 密码 -->
					<u-form-item prop="password" label="密码:" left-icon="lock">
						<u-input placeholder='请输入密码' type="password" v-model="loginFrom.password" />
					</u-form-item>
				</view>
			</u-form>

			<view class="tips" v-if="islogin">
				<u-icon name="error" color=" $u-content-color" size="28"></u-icon>
				未注册的用户请点击注册哦
			</view>

			<view class="tips" v-else>
				<u-icon name="error" color=" $u-content-color" size="28"></u-icon>
				感谢您注册
			</view>
			<u-button type="warning" v-if='islogin' @click="submit">登陆</u-button>
			<u-button type="warning" v-else @click="submit">注册</u-button>

			<view class="alternative">
				<view @click="switchState" v-if="islogin">注册</view>
				<view @click="switchState" v-else>返回登陆</view>
				<view @click="errorButton">遇到问题</view>
			</view>
		</view>
		<view class="buttom" v-if="islogin">
			<view class="loginType">
				<view class="wechat item" @click="weChatLogin">
					<view class="icon">
						<u-icon size="70" name="weixin-fill" color="rgb(83,194,64)"></u-icon>
					</view>
					微信
				</view>
				<view class="QQ item" @click="weChatLogin">
					<view class="icon">
						<u-icon size="70" name="qq-fill" color="rgb(17,183,233)"></u-icon>
					</view>
					QQ
				</view>
			</view>
			<view class="hint">
				登录代表同意
				<text class="link">寻觅用户协议、隐私政策，</text>
				并授权使用您的寻觅账号信息（如昵称、头像、收获地址）以便您统一管理
			</view>
		</view>
		<!-- 弹窗 -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				islogin: true, //判断是登陆还是注册

				loginFrom: {
					id: '', //账号
					password: '', //密码
					email: '', //邮箱
					nickname: '' //昵称

				},
				// 验证表单
				errorType: ['toast'], //toast
				loginFromRules: {

							id: [
						{
							required: true,
							message: '请填写帐号',
							trigger: ['change', 'blur']
						},

					],
					nickname: [

						{
							min: 4,
							message: '昵称不能小于4个字符哦',
							trigger: 'change'
						},
						{
							max: 8,
							message: '昵称不能超过8个字符哦',
							trigger: 'change',
						},

						{
							required: true,
							message: '请填写昵称',
							trigger: ['change', 'blur']
						},

					],
					password: [{
							min: 6,
							message: '密码不能小于6个字符呢',
							trigger: 'change'
						},
						{
							max: 12,
							message: '密码不能大于12个字符呢',
							trigger: 'change'
						},
						{
							required: true,
							message: '请填写密码',
							trigger: ['change', 'blur']
						},
					],
					email: [{
							required: true,
							message: '请填写邮箱号',
							trigger: ['change', 'blur']
						},
						{
							type: 'email',
							message: '邮箱号不正确哦',
							trigger: ['change', 'blur']
						},


					]
				}

			}
		},



		methods: {
			// 切换状态
			switchState() {
				this.islogin = !this.islogin;
				this.loginFrom.password = ''; //清空密码
			},
			// 登陆
			submit() {
				// console.log(this.$refs,'1111111')
				this.$refs.loginFrom.validate(valid => {
					if (valid) {
						console.log('验证通过');
						this.$u.route({
							url: 'pages/index/index'
						})
					} else {
						console.log('验证失败');
					}
				});
			},
			weChatLogin() {
				this.$refs.uToast.show({
					title: '此功能正在开发中',
					type: 'warning',
					position: 'top'
				})
			},
			// 提示联系作者
			errorButton() {
				this.$refs.uToast.show({
					title: '请联系作者QQ752449732',
					icon: false,
					type: 'warning',
					position: 'top'
				})
			},



		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.loginFrom.setRules(this.loginFromRules);
		}
	};
</script>

<style lang="scss" scoped>
	.wrap {
		font-size: 28rpx;

		.content {
			width: 600rpx;
			margin: 80rpx auto 0;

			.logo {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 100rpx;

				.title_img {
					display: flex;
					align-items: center;

					image {
						display: block;
						margin-left: 20rpx;
						width: 20px;
						height: 20px;
					}
				}

			}

			.title {
				text-align: left;
				font-size: 40rpx;
				font-weight: 500;

			}


			.tips {
				color: $u-type-info;
				margin-bottom: 60rpx;
				margin: 40rpx 0 60rpx 0;

			}

			.getCaptcha {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {

				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 60rpx;
			}
		}

		.buttom {
			.loginType {
				display: flex;
				padding: 160rpx 150rpx 150rpx 150rpx;
				justify-content: space-between;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: $u-content-color;
					font-size: 28rpx;
				}
			}

			.hint {
				padding: 20rpx 40rpx;
				font-size: 20rpx;
				color: $u-tips-color;

				.link {
					color: $u-type-warning;
				}
			}
		}
	}
</style>
